package com.jims.master.modules.doctor.netHosptial.service;


import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.doctor.netHosptial.dao.NetPrescMainDao;
import com.jims.master.modules.doctor.netHosptial.entity.NetClinicMaster;
import com.jims.master.modules.doctor.netHosptial.entity.NetPresc;
import com.jims.master.modules.doctor.netHosptial.entity.NetPrescMain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 处方主表Service
 * @author xueyx
 * @version 2017-04-06
 */
@Service
@Transactional(readOnly = true)
public class NetPrescMainService extends CrudService<NetPrescMainDao, NetPrescMain> {

	@Autowired
	private NetPrescService netPrescService;

	@Autowired
	private NetClinicMasterService netClinicMasterService;

	//todo
	/*@Autowired
	private HisSerialService hisSerialService;*/

	@Override
    public NetPrescMain get(String id) {
		return super.get(id);
	}
	
	@Override
    public List<NetPrescMain> findList(NetPrescMain netPrescMain) {
		return super.findList(netPrescMain);
	}
	
	@Override
    public Page<NetPrescMain> findPage(Page<NetPrescMain> page, NetPrescMain netPrescMain) {
		return super.findPage(page, netPrescMain);
	}
	
	@Transactional(readOnly = false)
	public void saveAll(NetPrescMain netPrescMain) {
		if(StringUtils.isEmpty(netPrescMain.getId())){
			NetClinicMaster clinicMaster = netClinicMasterService.get(netPrescMain.getClinicId());
			netPrescMain.setVisitDate(clinicMaster.getVisitDate());
			netPrescMain.setVisitNo(clinicMaster.getVisitNo());
			netPrescMain.setVisitId("0");
			//todo
			/*HisSerial hisSerial = new HisSerial();
			hisSerial.setType("2");
			netPrescMain.setPrescNo(hisSerialService.getNextNoByType(hisSerial));
			hisSerialService.updateBytype(hisSerial);
			HisSerial hisSerial2 = new HisSerial();
			hisSerial2.setType("3");
			netPrescMain.setSerialNo( hisSerialService.getNextNoByType(hisSerial2));
			hisSerialService.updateBytype(hisSerial2);*/
			netPrescMain.setChargeFllag("0");
			netPrescMain.setConfirmFlag("0");
		}
		super.save(netPrescMain);
		NetPrescMain netPrescMainsave = super.get(netPrescMain.getId());
		for(NetPresc entity:netPrescMain.getList()){
			//删除数据库保存过得数据
			if(StringUtils.isNotEmpty(entity.getId())&&StringUtils.isEmpty(entity.getDrugName())){
				netPrescService.delete(entity);
			}else {
				//判断为保存前的删除
				if (StringUtils.isNotEmpty(entity.getDrugName())){
					//不是新增加的初始化数据
					if (StringUtils.isEmpty(entity.getId())) {
						entity.setMainId(netPrescMainsave.getId());
						entity.setVisitDate(netPrescMainsave.getVisitDate());
						entity.setVisitNo(netPrescMainsave.getVisitNo());
						entity.setSerialNo(netPrescMainsave.getSerialNo());
						entity.setPrescNo(netPrescMainsave.getPrescNo());
						entity.setItemNo(String.valueOf(netPrescMain.getList().indexOf(entity) + 1));
						entity.setClinicId(netPrescMainsave.getClinicId());
						entity.setRemarks("0");
						entity.setPatientId(netPrescMainsave.getPatientId());
					}
					//todo orderno suborderno
				/*entity.setItemClass("");
				entity.setProvidedIndicator("");
				*/
					netPrescService.save(entity);
				}
			}
		}
	}
	
	@Override
    @Transactional(readOnly = false)
	public int delete(NetPrescMain netPrescMain) {
		return super.delete(netPrescMain);
	}

	public List<NetPrescMain> findByClinic(NetPrescMain netPrescMain){
		return dao.findByClinic(netPrescMain);
	}
	
}